package bean.resource;

import java.io.Serializable;
import java.util.List;

import dao.resource.ResourceDAO;
import entity.resource.Resource;
import entity.user.User;

import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.criteria.CriteriaQuery;

import bean.user.UserBean;

@Named
@Stateful
@ConversationScoped
public class ResourceBean implements Serializable {
	
	private static final long serialVersionUID = 12;
	
	@Inject
	private ResourceDAO resourceDAO;
	
	private Long id;
	
	@Inject
	private Conversation conversation;
	
	private Resource resource;
	
	public void load() {
		if (null == id) {
			resource = new Resource();
		} else {
			resource = resourceDAO.findById(id, false);
		}
	}
	
	public String save() {
		resourceDAO.makePersistent(resource);
		resourceDAO.flush();
		return "/content/resource/list?faces-redirect=true";
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public Resource getResource() {
		return resource;
	}
	
	public void setResource(Resource resource) {
		this.resource = resource;
	}
	
	public List<Resource> getAll()
	   {

	      CriteriaQuery<Resource> criteria = this.entityManager.getCriteriaBuilder().createQuery(Resource.class);
	      return this.entityManager.createQuery(criteria.select(criteria.from(Resource.class))).getResultList();
	   }

	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	private EntityManager entityManager;
	
    public String create()
    {
       this.conversation.begin();
       return "/content/resource/list?faces-redirect=true";
    }
	
    public String update()
    {
       this.conversation.end();

       try
       {
          if (this.id == null)
          {
             this.entityManager.persist(this.resource);
             return "/content/resource/list?faces-redirect=true";
          }
          else
          {
             this.entityManager.merge(this.resource);
        	 this.entityManager.flush();
             return "/content/resource/list?faces-redirect=true";
          }
       }
       catch (Exception e)
       {
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
          return null;
       }
    }
    
    public void retrieve()
    {

       if (FacesContext.getCurrentInstance().isPostback())
       {
          return;
       }

       if (this.conversation.isTransient())
       {
          this.conversation.begin();
       }

       if (this.id == null)
       {
          this.resource = new Resource();
       }
       else
       {
          this.resource = findById(getId());
       }
    }
    
	public Resource findById(Long id)
	{
	      return this.entityManager.find(Resource.class, id);
	}

	 	
	@javax.annotation.Resource
	private SessionContext sessionContext;

	public Converter getConverter()
	{

	   final ResourceBean ejbProxy = this.sessionContext.getBusinessObject(ResourceBean.class);

	      return new Converter()
	      {

	         @Override
	         public Object getAsObject(FacesContext context, UIComponent component, String value)
	         {
	            return ejbProxy.findById(Long.valueOf(value));
	         }

	         @Override
	         public String getAsString(FacesContext context, UIComponent component, Object value)
	         {
	            if( value != null && value instanceof Resource )
	            {
	            	return String.valueOf(((Resource) value).getId());
	            }
	            return "";
	         }
	      };
	   }
}
